<?php
//load CSVReader class
Loader::loadClass('CSVReader', "modules/InnoForum/pnincludes");
 /**
  * initialise module
  *
  */
function InnoForum_init() {
  if (!DBUtil::createTable('innoforum_setting')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_type')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_category')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_subcategory')) {
      return false;
  }

  //Add new 
  if (!DBUtil::createTable('innoforum_brand')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_model')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_carproperty')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_subcategory_brand')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_subcategory_carproperty')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_subcategory_model')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_finance')) {
      return false;
  }

  if (!DBUtil::createTable('innoforum_level')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_topics')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_posts')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_resource_image')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_favourite')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_teamtopic')) {
      return false;
  }
  if (!DBUtil::createTable('innoforum_amount_topic')) {
      return false;
  }

  init_setting();
  init_type();
  init_category();
  init_subcategory();
  init_level();

  createFilesDirectory();
  return true;
}

function InnoForum_delete() {
  // drop table
  DBUtil::dropTable('innoforum_setting');
  DBUtil::dropTable('innoforum_type');
  DBUtil::dropTable('innoforum_category');
  DBUtil::dropTable('innoforum_subcategory');

  DBUtil::dropTable('innoforum_brand');
  DBUtil::dropTable('innoforum_model');
  DBUtil::dropTable('innoforum_carproperty');
  DBUtil::dropTable('innoforum_subcategory_brand');
  DBUtil::dropTable('innoforum_subcategory_model');
  DBUtil::dropTable('innoforum_subcategory_carproperty');
  DBUtil::dropTable('innoforum_finance');

  DBUtil::dropTable('innoforum_level');
  DBUtil::dropTable('innoforum_topics');
  DBUtil::dropTable('innoforum_posts');
  DBUtil::dropTable('innoforum_resource_image');
  DBUtil::dropTable('innoforum_favourite');
  DBUtil::dropTable('innoforum_teamtopic');
  DBUtil::dropTable('innoforum_amount_topic');

  rmdir_recurse(DELETE_FILES_MAINPATH);
  return true;
}

function init_setting(){
  $dataArray = CSVReader::readcsv('modules/InnoForum/data/setting.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'innoforum_setting', true);
    unset($dataArray);
  }
}
function init_type(){
  $dataArray = CSVReader::readcsv('modules/InnoForum/data/type.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'innoforum_type', true);
    unset($dataArray);
  }
}
function init_category(){
    $dataArray= array("0"=>array("id"=>"1",
                          "formtype"=>"CAR",
                          "name"=>"รถบัส",
                          "prefix"=>"BS"
                          ),
                      "1"=>array("id"=>"2",
                                  "formtype"=>"CAR",
                                  "name"=>"รถบรรทุก",
                                  "prefix"=>"BTK"
                                  ),
                      "2"=>array("id"=>"3",
                                  "formtype"=>"CAR",
                                  "name"=>"รถตู้",
                                  "prefix"=>"BVN"
                                  ),
                      "3"=>array("id"=>"4",
                                  "formtype"=>"CAR",
                                  "name"=>"รถปิกอัพ",
                                  "prefix"=>"BPK"
                                  ),
                      "4"=>array("id"=>"5",
                                  "formtype"=>"CAR",
                                  "name"=>"เครื่องจักรกลหนัก",
                                  "prefix"=>"BMN"
                                  ),
                      "5"=>array("id"=>"6",
                                  "formtype"=>"OTHER",
                                  "name"=>"อะไหล่รถยนต์",
                                  "prefix"=>"BSP"
                                  ),
                      "6"=>array("id"=>"7",
                                  "formtype"=>"OTHER",
                                  "name"=>"ชุดแต่งรถ",
                                  "prefix"=>"BAC"
                                  ),
                      "7"=>array("id"=>"8",
                                  "formtype"=>"OTHER",
                                  "name"=>"บริการต่างๆ",
                                  "prefix"=>"BSV"
                                  ),
                      "8"=>array("id"=>"9",
                                  "formtype"=>"OTHER",
                                  "name"=>"งาน",
                                  "prefix"=>"BJB"
                                  )
                    );
    if ($dataArray){
      DBUtil::insertObjectArray($dataArray, 'innoforum_category', true);
      unset($dataArray);
    }
}

function init_subcategory(){
  $dataArray = CSVReader::readcsv('modules/InnoForum/data/subcategory.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'innoforum_subcategory', true);
    unset($dataArray);
  }
}

function init_level(){
  $dataArray = CSVReader::readcsv('modules/InnoForum/data/level.csv');
  if ($dataArray){
    DBUtil::insertObjectArray($dataArray, 'innoforum_level', true);
    unset($dataArray);
  }
}

function createFilesDirectory(){
    //Make [resource] directory
  if (!is_dir(UPLOAD_FILES_MAINPATH)) {
    mkdir(UPLOAD_FILES_MAINPATH, 0777);
  }
  //Make [innoforum_upload] directory
  if (!is_dir(UPLOAD_FILES_SUBPATH)) {
    mkdir(UPLOAD_FILES_SUBPATH, 0777);
  }
  //Make image  directory
  if (!is_dir(UPLOAD_IMAGE_ORIGINAL)) {
    mkdir(UPLOAD_IMAGE_ORIGINAL, 0777);
  }
  //Make thumb directory
  if (!is_dir(UPLOAD_IMAGE_THUMB)) {
    mkdir(UPLOAD_IMAGE_THUMB, 0777);
  }

  //copy .htaccess and index.html into resource directory
  copy(HTACCESS_PATH,UPLOAD_FILES_MAINPATH_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_FILES_MAINPATH_HTML_BLANK_PATH);

  //copy .htaccess and index.html into innoforum_upload directory
  copy(HTACCESS_PATH,UPLOAD_FILES_SUBPATH_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_FILES_SUBPATH_HTML_BLANK_PATH);

  //copy .htaccess and index.html into large directory
  copy(HTACCESS_PATH,UPLOAD_IMAGE_ORIGINAL_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_IMAGE_ORIGINAL_HTML_BLANK_PATH);

  //copy .htaccess and index.html into medium directory
  copy(HTACCESS_PATH,UPLOAD_IMAGE_THUMB_HTACCESS_PATH);
  copy(HTML_BLANK_PATH,UPLOAD_IMAGE_THUMB_HTML_BLANK_PATH);

}

function rmdir_recurse($path)
{
    $path= rtrim($path, '/').'/';
    $handle = opendir($path);
    for (;false !== ($file = readdir($handle));)
        if($file != "." and $file != ".." )
        {
            $fullpath= $path.$file;
            if( is_dir($fullpath) )
            {
                rmdir_recurse($fullpath);
                rmdir($fullpath);
            }
            else
              unlink($fullpath);
        }
    closedir($handle);
}
